/** ============================================================================================== **/
/**						TANK MANAGER											 				   **/	
/** ============================================================================================== **/

BootApplication.controller("PreferTankController",
			[ "$scope",	'$translate', "NgTableParams", "PreferTankService", '$http', 'configuration',
	 function ($scope, 	 $translate,   NgTableParams,   preferTankService,   $http,   configuration) {
	
	console.log("PreferTankController");			
	/** INIZIALIZZAZIONI ********************************************************************************************************************************/
	$scope.tankList						= [];
	$scope.filtriAttivi					= {};
	$scope.displayProp					= {};
	/****************************************************************************************************************************************************/
	
	/*******************************************************************************************************	GESTIONE FILTER	   ***********	START   */
	$scope.showFilter					= false;
	$scope.hideShowFilter = function () {
		$scope.showFilter = !$scope.showFilter;
	}
	
	$scope.activeFilter = function () {
		console.log("det-tank-ctrl.js		>>>	Applico i filtri");
		$scope.getTankList();
	}
	
	/*******************************************************************************************************	GESTIONE FILTER	   ***********	END     */
	
	
	/*******************************************************************************************************	FIGURE VIEW		   ***********	START   */
	$scope.bigCurrentPage 				= 1;
	$scope.itemsPerPage 				= 8;
	$scope.maxSize 						= 5;
	
	$scope.getTankList = function () {
		console.log("det-tank-ctrl.js		>>>	Recupero la lista delle vasche");
		$scope.displayProp.paginate			= true;
		$scope.displayProp.displayStart		= $scope.bigCurrentPage==1 ? 0 : $scope.itemsPerPage*($scope.bigCurrentPage-1);
		$scope.displayProp.displayLength	= $scope.itemsPerPage;
		preferTankService.getTankList( $scope.filtriAttivi, $scope.displayProp ).then(function( response ){
			if (response!=null) {
				$scope.tankList			= response.data.aaData;
				$scope.bigTotalItems 	= response.data.iTotalRecords;
			}
		});
	}
	$scope.getTankList();
	
	$scope.setPage = function (pageNo) {
		$scope.bigCurrentPage = pageNo;
		$scope.getTankList();
	};
	$scope.pageChanged = function() {
		$scope.getTankList();
	};
	/*******************************************************************************************************	FIGURE VIEW		   ***********	END     */
	
	
	/*******************************************************************************************************	TABLE VIEW 		   ***********	START	*/
	/** Definizione delle Label internazionalizzate per l'intestazione della tabella */
    $scope.tableHeader = {};
    $scope.tableHeader.id 				= "Id";
    $scope.tableHeader.note 			= "Note";
    $scope.tableHeader.larghezza		= "Larghezza";
    $scope.tableHeader.lunghezza		= "Lunghezza";
    $scope.tableHeader.altezza			= "Altezza";
    $scope.tableHeader.capacita			= "Capacita";
	/** Funzione che chiama il servizio di ricerca delle stazioni e popola il datatable. */
    $scope.tableParams = new NgTableParams({
		  sorting: { 1 : "asc" }, 	// initial sort order
		  page: 1,			  		// show first page 
	      count: 10					// count per page
		}, 
		{
		  counts: [5, 10, 25],
	      paginationMaxBlocks: 4,
	      paginationMinBlocks: 3,
		  getData: function(params) {
			  if ($scope.showTable) {
				  
				  $scope.displayProp.paginate			= true;
				  $scope.displayProp.displayStart		= (params.url().page -1) * params.url().count;
				  $scope.displayProp.displayLength		= params.url().count;
				  return preferTankService.getTankList( $scope.filtriAttivi, $scope.displayProp ).then(function( response ) {
					  if (response!=null && response.data!=null)
						  params.total(response.data.iTotalRecords);
					  else 
						  params.total(0);
					  return response.data.aaData;
				  }, function errorCallback(response) {
					  params.total(0);
					  return new Array();
				  });
			  }
		 }
	});
    /*******************************************************************************************************	TABLE VIEW 		   ***********	END		*/
	
	
	/*******************************************************************************************************	CHANGE TAB 		   ***********	START	*/
	$scope.showFigure		= true;
	$scope.showTable		= false;
	
	$scope.activeFigure = function(event){
		event.preventDefault();
		$scope.showFigure=true;
		$scope.showTable=false;
	}
	$scope.activeTable = function(event){
		event.preventDefault();
		$scope.showFigure=false;
		$scope.showTable=true;
		$scope.tableParams.reload();
	}
	/*******************************************************************************************************	CHANGE TAB 		   ***********	END		*/
}]);